<?
function page_load ()
{
	if (isset($HTTP_RAW_POST_DATA)) {                                                                                                                                  
		$input = $HTTP_RAW_POST_DATA;                                                                                                                                  
	} else if (strlen($HTTP_POST_VARS["input"])>0) {                                                                                                                                  
		$input = stripslashes($HTTP_POST_VARS["input"]);                                                                                                                                  
	} else {                                                                                                                                  
		$input = implode("\r\n", file('php://input'));                                                                                                                                  
	} 

	if ($dom=domxml_open_mem($input))                                                                                                                                
	{  
                $o = $dom->document_element();  
		if ($o->node_name() == 'savefile')
		{
			$path=$o->get_attribute ('datapath');
			$text=$o->get_content ();
			
			
			$ret = '';
			for ($e=0;$e<strlen($text);$e++)
				if (ord(substr($text,$e,1)) < 194)
					$ret.=substr($text,$e,1);
			
			
			
			SaveFile ($path,$ret);
			echo $path . ' was saved';
			exit;
		}
	}
	
	$action = $_GET["action"];
	$path 	= $_GET["path"];
	if ($action=='getfolder')
	{
		header ('Content-type: text/xml');
		echo GetFolder ($path);
	}
	else if ($action=='createfile')
	{ 
		echo SaveFile ($path, base64_encode("New file " . date ('m-d-Y H:i:s')));
	}
	else if ($action=='getfileb')
	{ 
		$fr= GetFile ($path);
		Highlight( base64_decode ($fr), $path );
	}
	else if ($action=='getfile')
	{ 
		echo GetFile ($path);
	}
	else if ($action=='deletefile')
	{ 
		echo DeleteFile ($path);
	}
        else if ($action=='createfolder')
	{ 
		echo CreateFolder ($path);
	}
        else if (strlen($action) > 0)
        {
                echo 'action "' . $action . '" not defined';
        }
}

foreach( $_FILES as $file_name => $file_array )               
{        
	echo 'Processing...';        
	$docroot 	 = $_SERVER['DOCUMENT_ROOT'];
	$upload_path = $file_array['tmp_name'];              
	$upload_name = $file_array['name'];              
	$upload_type = $file_array['type'];              
	$upload_size = $file_array['size'];              
	if ( is_uploaded_file( $upload_path )  )               
	{              
		$tmp = $docroot . $_POST['uploadTarget'] . "/". $upload_name;              
		copy($upload_path, $tmp) or die("unable to create file: " . $tmp);
		echo 'Upload complete';
		exit;                 
	}                
}    

page_load ();


function xhtml_highlight($str) 
{               
    global $this_doc_path;  
    if (preg_match ('/(.*)\.js$/',$this_doc_path))   
    {   
        $php_str = '<' . '?' . $str . '?' . '>';   
        $hlt = highlight_string(stripslashes($php_str), true);   
        $hlt = str_replace ('&lt;?','', str_replace ('?&gt;','', $hlt)); 
		$t=''; //substr($hlt,2);   
        return true;   
    }               
    highlight_string($str);               
    return true;               
}               

function Highlight ($str, $path='')
{	       
    echo "<style>\n";
    echo "body { margin:0px;padding:0px; }\n";
    echo "code { font-family:'DejaVu Sans Mono','URW Chancery L';font-size:8pt }\n";
    echo "</style>\n";
    echo '<table border="0"><tr><td nowrap>';
	
    if (preg_match ('/(.*)\.js$/',$path))   
    {   
        $php_str = '<' . '?' . $str . '?' . '>';   
        $hlt = highlight_string(stripslashes($php_str), true);   
        $hlt = str_replace ('&lt;?','', str_replace ('?&gt;','', $hlt));  
        echo $hlt;
    } else highlight_string($str);   
	
    echo '</td></tr></table>'; 
}               

function DeleteFile ($path)
{	
	$docroot = $_SERVER['DOCUMENT_ROOT'];
    $address = $docroot . $path;
	unlink ($address);
}

function CreateFolder ($path)
{	
	$docroot = $_SERVER['DOCUMENT_ROOT'];
    $address = $docroot . $path;
	mkdir ($address);
	return "$path was created";
}

function SaveFile ($path,$text)
{	
	$docroot = $_SERVER['DOCUMENT_ROOT'];
    $address = $docroot . $path;
	$fp=fopen ($address, 'w');
	fwrite ($fp,base64_decode($text));
	fclose ($fp);
	return true;
}

function GetFile ($path)
{	
	$docroot = $_SERVER['DOCUMENT_ROOT'];
    $address = $docroot . $path;  
	$fp=fopen ($address, 'r');
	$fr=fread ($fp,filesize($address));
	fclose ($fp);
	return base64_encode($fr);
}

function GetFolder ($path)
{
        /* TO DO: Add processing code here */  
		$docroot 	= $_SERVER['DOCUMENT_ROOT'];
		$address	= $docroot . $path;
        $realpath 	= realpath($address);  
        $mod_date 	= date("F d Y h:i:s A", filemtime ($address));    
        $temp_arr 	= explode ("/", $realpath);  
        $dir_name 	= array_pop($temp_arr);  
        $parent   	= implode ("/", $temp_arr);    
		$relative 	= str_replace ($docroot,'',$address);
        $xml  = "<directory parent=\"" . realpath('.') . "\" modified=\"" . $mod_date . "\" name=\"" . $dir_name . "\" path=\"" . $relative . "\">\n";    
        $dh   = opendir( $address ) or die ("couldn't open directory");   
        
		$files=array();
		$folders=array();
		
		
		
		while (! ( ( $file = readdir ( $dh ) ) == false ) )   
        {  
            $fullpath = "$address/$file";//realpath("$realpath/$file");   
			$relative = str_replace ($docroot,'',$fullpath);
			$relative = str_replace ('//','/',$relative);
            if ( is_dir( $fullpath ) )   
            {   
                if (! ($file == "." || $file == "..") )   
                {  
					$sub=0;
					$ds = @opendir( $fullpath );  
					while (! ( ( $fs = readdir ( $ds ) ) == false ) )   
					{
						 if ( (! ($fs == "." || $fs == "..") ) && is_dir ($fullpath.'/'.$fs) ) 
						 {
						 	$sub++;break;
						 }  
					} 
					$folders[] = array ('name'=>htmlspecialchars( $file ),
										'count'=>$sub,
										'path'=>htmlspecialchars( $relative ) 
										);
					
//					$xml .= "<folder name=\"" . htmlspecialchars( $file ) . "\" count='".$sub."' path=\"" . htmlspecialchars( $relative ) . "\"/>\n"; 
                }  
            }   
            else   
            {      
                $info    = pathinfo($fullpath);  
				$files[] = array ('name'=>htmlspecialchars( $file ),
									'path'=>htmlspecialchars( $relative ),
									'type'=>strtolower($info["extension"]) ,
									'size'=>filesize($fullpath) ,
									'date'=>date("F d Y h:i:s A", filemtime ($fullpath)) 
									);
            }      
        }  
		
		
		uasort($folders, 'Compare');
		uasort($files, 'Compare');
		
		foreach ($folders as $i) $xml.= MakeNode('folder', $i); 
		foreach ($files as $i) $xml.= MakeNode('file', $i); 
		
        closedir( $dh );   
        $xml .= "</directory>";   
		return $xml;
}

function MakeNode($name, $array) 
{ 
	$xml='<'.$name.' ';
	while (list ($n,$v)=each ($array))
	{
		$xml.= ' '.$n.'="' . $v . '"';
	}
	$xml.='/>\n';
	return $xml;
}

function Compare($ar1, $ar2) 
{ 
   if (strtolower($ar1['name'])<strtolower($ar2['name'])) 
      return -1; 
   else if (strtolower($ar1['name'])>strtolower($ar2['name'])) 
      return 1;  
   return 0; 
} 

?>